/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.wsgi.mail;

import static cc.aileron.commons.utility.ResourceUtils.resource;
import static cc.aileron.wsgi.navigation.WsgiNavigation.State.Continue;

import java.io.PrintWriter;
import java.io.StringWriter;

import cc.aileron.commons.accessor.PojoAccessor;
import cc.aileron.commons.accessor.PojoAccessorFactory;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.mail.sender.MailSender;
import cc.aileron.mail.sender.MailTo;
import cc.aileron.template.exception.CompileException;
import cc.aileron.template.xml.XmlTemplate;
import cc.aileron.template.xml.XmlTemplateCompiler;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.navigation.WsgiNavigation;

import com.google.inject.Inject;

/**
 * WsgiMailProcessProvider
 * 
 * @author aileron
 * 
 */
public class WsgiMailProcessProviderImpl implements WsgiMailProcessProvider
{
    /*
     * (非 Javadoc)
     * 
     * @see
     * cc.aileron.wsgi.mail.WsgiMailProcessProvider#get(cc.aileron.wsgi.mail
     * .WsgiMailSetting)
     */
    @Override
    public <T> WsgiProcess<T> get(final WsgiMailSetting setting)
            throws CompileException, ResourceNotFoundException
    {
        final XmlTemplate template = xml.compile(resource(
                setting.templateFilePath).toFileContent());
        return new WsgiProcess<T>()
        {
            @Override
            public WsgiNavigation doProcess(final T p) throws Exception
            {
                final PojoAccessor<T> pojo = factory.accessor(p);
                final MailTo to = (MailTo) pojo
                        .getObject(setting.propertieName);

                final StringWriter str = new StringWriter();
                final PrintWriter out = new PrintWriter(str);

                template.parse(out, pojo);
                out.close();
                to.text = str.toString();

                mailSender.sendMail(to);

                return Continue.navigation();
            }
        };
    }

    /**
     * @param mailSender
     * @param xml
     * @param factory
     */
    @Inject
    public WsgiMailProcessProviderImpl(final MailSender mailSender,
            final XmlTemplateCompiler xml, final PojoAccessorFactory factory)
    {
        this.xml = xml;
        this.mailSender = mailSender;
        this.factory = factory;
    }

    /**
     * xml-compiler
     */
    private final XmlTemplateCompiler xml;

    /**
     * factory accessor
     */
    final PojoAccessorFactory factory;

    /**
     * mail-sender
     */
    final MailSender mailSender;
}
